<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of read_sms_mesh</title>
  <meta name="keywords" content="read_sms_mesh">
  <meta name="description" content="Read sms mesh files into Matlab mesh object">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html v1.5 &copy; 2003-2005 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../index.html">Home</a> &gt;  <a href="index.html">fvcom_prepro</a> &gt; read_sms_mesh.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="index.html">Index for fvcom_prepro&nbsp;<img alt=">" border="0" src="../right.png"></a></td></tr></table>-->

<h1>read_sms_mesh
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>Read sms mesh files into Matlab mesh object</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>function [Mobj] = read_sms_mesh(varargin) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre class="comment"> Read sms mesh files into Matlab mesh object  

 [Mobj] = function read_fvcom_mesh(varargin)

 DESCRIPTION:
    Read SMS 2dm file and bathymetry file 
    Store in a matlab mesh object 

 INPUT [keyword pairs]:  
   '2dm'                   = sms 2dm file [e.g. tst_grd.dat] 
   [optional] 'bath'       = sms bathymetry file [e.g. tst_dep.dat] 
   [optional] 'coordinate' = coordinate system [spherical; cartesian (default)]
   [optional] 'project'    = generate (x,y) coordinates if input is (lon,lat) 
                             generate (lon,lat) coordinates if input is (x,y)
                            ['true' ; 'false' (default)], see myproject.m
   [optional] 'addCoriolis' = calculate Coriolis param (f), requires [lon,lat]

 OUTPUT:
    Mobj = matlab structure containing mesh data

 EXAMPLE USAGE
    Mobj = read_sms_mesh('2dm','skagit.2dm','bath','bathy.dat')

 Author(s):  
    Geoff Cowles (University of Massachusetts Dartmouth)

 Revision history
   
==============================================================================</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../matlabicon.gif)">
<li><a href="make_blank_mesh.html" class="code" title="function [Mobj] = make_blank_mesh">make_blank_mesh</a>	Make a blank mesh object with default params</li><li><a href="my_project.html" class="code" title="function [out_east,out_north] = my_project(in_east,in_north,direction)">my_project</a>	Sample user-defined projection and inverse projection of (lon,lat) to (x,y)</li></ul>
This function is called by:
<ul style="list-style-image:url(../matlabicon.gif)">
<li><a href="example.html" class="code" title="">example</a>	example demonstrating reading in a 2DM file and constructing a model</li></ul>
<!-- crossreference -->



<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function [Mobj] = read_sms_mesh(varargin) </a>
0002 
0003 <span class="comment">% Read sms mesh files into Matlab mesh object</span>
0004 <span class="comment">%</span>
0005 <span class="comment">% [Mobj] = function read_fvcom_mesh(varargin)</span>
0006 <span class="comment">%</span>
0007 <span class="comment">% DESCRIPTION:</span>
0008 <span class="comment">%    Read SMS 2dm file and bathymetry file</span>
0009 <span class="comment">%    Store in a matlab mesh object</span>
0010 <span class="comment">%</span>
0011 <span class="comment">% INPUT [keyword pairs]:</span>
0012 <span class="comment">%   '2dm'                   = sms 2dm file [e.g. tst_grd.dat]</span>
0013 <span class="comment">%   [optional] 'bath'       = sms bathymetry file [e.g. tst_dep.dat]</span>
0014 <span class="comment">%   [optional] 'coordinate' = coordinate system [spherical; cartesian (default)]</span>
0015 <span class="comment">%   [optional] 'project'    = generate (x,y) coordinates if input is (lon,lat)</span>
0016 <span class="comment">%                             generate (lon,lat) coordinates if input is (x,y)</span>
0017 <span class="comment">%                            ['true' ; 'false' (default)], see myproject.m</span>
0018 <span class="comment">%   [optional] 'addCoriolis' = calculate Coriolis param (f), requires [lon,lat]</span>
0019 <span class="comment">%</span>
0020 <span class="comment">% OUTPUT:</span>
0021 <span class="comment">%    Mobj = matlab structure containing mesh data</span>
0022 <span class="comment">%</span>
0023 <span class="comment">% EXAMPLE USAGE</span>
0024 <span class="comment">%    Mobj = read_sms_mesh('2dm','skagit.2dm','bath','bathy.dat')</span>
0025 <span class="comment">%</span>
0026 <span class="comment">% Author(s):</span>
0027 <span class="comment">%    Geoff Cowles (University of Massachusetts Dartmouth)</span>
0028 <span class="comment">%</span>
0029 <span class="comment">% Revision history</span>
0030 <span class="comment">%</span>
0031 <span class="comment">%==============================================================================</span>
0032 
0033 subname = <span class="string">'read_sms_mesh'</span>;
0034 <span class="keyword">global</span> ftbverbose;
0035 <span class="keyword">if</span>(ftbverbose);
0036   fprintf(<span class="string">'\n'</span>)
0037   fprintf([<span class="string">'begin : '</span> subname <span class="string">'\n'</span>])
0038 <span class="keyword">end</span>;
0039 
0040 userproject = false;
0041 have_bath = false;
0042 
0043 <span class="comment">%------------------------------------------------------------------------------</span>
0044 <span class="comment">% Create a blank mesh object</span>
0045 <span class="comment">%------------------------------------------------------------------------------</span>
0046 Mobj = <a href="make_blank_mesh.html" class="code" title="function [Mobj] = make_blank_mesh">make_blank_mesh</a>();
0047 coordinate = <span class="string">'cartesian'</span>;
0048 
0049 <span class="comment">%------------------------------------------------------------------------------</span>
0050 <span class="comment">% Parse input arguments</span>
0051 <span class="comment">%------------------------------------------------------------------------------</span>
0052 
0053 <span class="keyword">if</span>(mod(length(varargin),2) ~= 0)
0054     error(<span class="string">'incorrect usage of read_sms_mesh, use keyword pairs'</span>)
0055 <span class="keyword">end</span>;
0056 
0057 
0058 <span class="keyword">for</span> i=1:2:length(varargin)-1
0059     keyword  = lower(varargin{i});
0060     <span class="keyword">if</span>( ~ischar(keyword) )
0061         error(<span class="string">'incorrect usage of read_sms_mesh'</span>)
0062     <span class="keyword">end</span>;
0063     
0064     <span class="keyword">switch</span>(keyword(1:3))
0065     
0066     <span class="keyword">case</span>'2dm'
0067         sms_2dm = varargin{i+1};
0068         have_2dm = true;
0069     <span class="keyword">case</span> <span class="string">'bat'</span>
0070         sms_bath = varargin{i+1};
0071         have_bath = true;
0072     <span class="keyword">case</span> <span class="string">'coo'</span>
0073         coord = varargin{i+1}
0074         <span class="keyword">if</span>(coord(1:3)==<span class="string">'sph'</span>)
0075             coordinate = <span class="string">'spherical'</span>
0076             have_lonlat = true;
0077         <span class="keyword">else</span>
0078             coordinate = <span class="string">'cartesian'</span>
0079             have_xy    = true;
0080         <span class="keyword">end</span>;
0081     <span class="keyword">case</span> <span class="string">'pro'</span>
0082         val = varargin{i+1};
0083         <span class="keyword">if</span>( val )
0084             userproject = true;
0085         <span class="keyword">else</span>
0086             userproject = false;
0087         <span class="keyword">end</span>;
0088     <span class="keyword">case</span> <span class="string">'add'</span>
0089         val = varargin{i+1};
0090         <span class="keyword">if</span>( val )
0091             addCoriolis = true;
0092         <span class="keyword">else</span>
0093             addCoriolis = false;
0094         <span class="keyword">end</span>;
0095     <span class="keyword">otherwise</span>
0096         error([<span class="string">'Can''t understand property:'</span> varargin{i+1}]);
0097     <span class="keyword">end</span>; <span class="comment">%switch(keyword)</span>
0098     
0099 <span class="keyword">end</span>;
0100         
0101 <span class="comment">%------------------------------------------------------------------------------</span>
0102 <span class="comment">% Read the mesh from the 2dm file</span>
0103 <span class="comment">%------------------------------------------------------------------------------</span>
0104 
0105 
0106 fid = fopen(sms_2dm,<span class="string">'r'</span>);
0107 <span class="keyword">if</span>(fid  &lt; 0)
0108     error([<span class="string">'file: '</span> sms_2dm <span class="string">' does not exist'</span>]);
0109 <span class="keyword">end</span>;
0110 
0111 <span class="comment">% Count number of elements and vertices</span>
0112 <span class="keyword">if</span>(ftbverbose);
0113   fprintf([<span class="string">'reading from: '</span> sms_2dm <span class="string">'\n'</span>])
0114   fprintf(<span class="string">'first pass to count number of nodes and vertices\n'</span>)
0115 <span class="keyword">end</span>;
0116 nElems = 0;
0117 nVerts = 0;
0118 lin = fgetl(fid); <span class="comment">%header</span>
0119 StillReading = true;
0120 <span class="keyword">while</span> StillReading 
0121     lin = fgetl(fid);
0122     <span class="keyword">if</span>(lin(1:2) == <span class="string">'E3'</span>)
0123         nElems = nElems + 1;
0124     <span class="keyword">elseif</span>(lin(1:2) == <span class="string">'ND'</span>)
0125         nVerts = nVerts + 1;
0126     <span class="keyword">else</span>
0127         StillReading = false;
0128     <span class="keyword">end</span>;
0129 <span class="keyword">end</span>;
0130 fclose(fid); fid = fopen(sms_2dm,<span class="string">'r'</span>);
0131 
0132 <span class="keyword">if</span>(ftbverbose); 
0133   fprintf(<span class="string">'nVerts: %d\n'</span>,nVerts); 
0134   fprintf(<span class="string">'nElems: %d\n'</span>,nElems); 
0135   fprintf(<span class="string">'reading in connectivity and grid points\n'</span>)
0136 <span class="keyword">end</span>;
0137 
0138 <span class="comment">% allocate memory to hold mesh and connectivity</span>
0139 tri = zeros(nElems,3);
0140 x   = zeros(nVerts,1);
0141 y   = zeros(nVerts,1);
0142 h   = zeros(nVerts,1);
0143 lon = zeros(nVerts,1);
0144 lat = zeros(nVerts,1);
0145 ts  = zeros(nVerts,1);
0146 
0147 lin=fgetl(fid); <span class="comment">%header</span>
0148 <span class="keyword">for</span> i=1:nElems
0149     C = textscan(fid, <span class="string">'%s %d %d %d %d %d'</span>, 1);
0150     tri(i,1) = C{3};
0151     tri(i,2) = C{4};
0152     tri(i,3) = C{5};
0153 <span class="keyword">end</span>;
0154 
0155 <span class="keyword">for</span> i=1:nVerts 
0156     C = textscan(fid, <span class="string">'%s %d %f %f %f '</span>, 1);
0157     x(i) = C{3};
0158     y(i) = C{4};
0159 <span class="keyword">end</span>;
0160 
0161 have_lonlat = false;
0162 have_xy     = false;
0163 <span class="keyword">if</span>(coordinate(1:5) == <span class="string">'spher'</span>)
0164     lon = x;
0165     lat = y;
0166     x = x*0.0;
0167     y = y*0.0;
0168     have_lonlat = true;
0169 <span class="keyword">else</span>
0170     have_xy = true;
0171 <span class="keyword">end</span>;
0172 
0173 
0174 <span class="comment">%------------------------------------------------------------------------------</span>
0175 <span class="comment">% Read the topography from the bathymetry file</span>
0176 <span class="comment">%------------------------------------------------------------------------------</span>
0177 
0178 <span class="keyword">if</span>(have_bath)
0179     fid = fopen(sms_bath,<span class="string">'r'</span>);
0180     <span class="keyword">if</span>(fid  &lt; 0)
0181         error([<span class="string">'file: '</span> sms_bath <span class="string">' does not exist'</span>]);
0182     <span class="keyword">else</span>
0183         <span class="keyword">if</span>(ftbverbose); fprintf(<span class="string">'reading sms bathymetry from: %s\n'</span>,sms_bath); <span class="keyword">end</span>;
0184     <span class="keyword">end</span>;
0185     lin=fgetl(fid);
0186     lin=fgetl(fid);
0187     lin=fgetl(fid);
0188     C = textscan(fid, <span class="string">'%s %d'</span>, 1);
0189     nVerts_tmp = C{2};
0190     C = textscan(fid, <span class="string">'%s %d'</span>, 1);
0191     nElems_tmp = C{2};
0192     <span class="keyword">if</span>( (nVerts-nVerts_tmp)*(nElems-nElems_tmp) ~= 0)
0193         fprintf(<span class="string">'dimensions of bathymetry file do not match 2dm file\n'</span>)
0194         fprintf(<span class="string">'bathymetry nVerts: %d\n'</span>,nVerts_tmp)
0195         fprintf(<span class="string">'bathymetry nElems: %d\n'</span>,nElems_tmp)
0196         error(<span class="string">'stopping...'</span>)
0197     <span class="keyword">end</span>;
0198     lin=fgetl(fid);
0199     lin=fgetl(fid);
0200     lin=fgetl(fid);
0201     <span class="keyword">for</span> i=1:nVerts
0202       C = textscan(fid, <span class="string">'%f'</span>, 1);
0203       h(i) = C{1}; 
0204     <span class="keyword">end</span>;
0205     have_bath = true;
0206 <span class="keyword">end</span>;
0207 
0208 <span class="comment">%------------------------------------------------------------------------------</span>
0209 <span class="comment">% Project if desired by user</span>
0210 <span class="comment">%------------------------------------------------------------------------------</span>
0211 
0212 <span class="keyword">if</span>(userproject)
0213     <span class="keyword">if</span>(coordinate(1:3) == <span class="string">'car'</span>)
0214         fprintf(<span class="string">'inverse projecting to get (lon,lat)\n'</span>)
0215         [lon,lat] = <a href="my_project.html" class="code" title="function [out_east,out_north] = my_project(in_east,in_north,direction)">my_project</a>(x,y,<span class="string">'inverse'</span>);
0216         have_lonlat = true;
0217     <span class="keyword">else</span>
0218         fprintf(<span class="string">'forward projecting to get (x,y)\n'</span>)
0219         [x,y] = <a href="my_project.html" class="code" title="function [out_east,out_north] = my_project(in_east,in_north,direction)">my_project</a>(lon,lat,<span class="string">'forward'</span>);
0220         have_xy = true;
0221     <span class="keyword">end</span>;
0222 <span class="keyword">end</span>;
0223 
0224 <span class="comment">%------------------------------------------------------------------------------</span>
0225 <span class="comment">% Transfer to Mesh structure</span>
0226 <span class="comment">%------------------------------------------------------------------------------</span>
0227 
0228 Mobj.nVerts  = nVerts;
0229 Mobj.nElems  = nElems;
0230 Mobj.nativeCoords = coordinate;
0231 
0232 <span class="keyword">if</span>(have_lonlat)
0233     Mobj.have_lonlat  = have_lonlat;
0234 <span class="keyword">end</span>;
0235 <span class="keyword">if</span>(have_xy)
0236     Mobj.have_xy      = have_xy;
0237 <span class="keyword">end</span>;
0238 <span class="keyword">if</span>(have_bath)
0239     Mobj.have_bath    = have_bath;
0240 <span class="keyword">end</span>;
0241 Mobj.x            = x;
0242 Mobj.y            = y;
0243 Mobj.ts           = ts;
0244 Mobj.lon          = lon;
0245 Mobj.lat          = lat;
0246 Mobj.h            = h;
0247 Mobj.tri          = tri;
0248 
0249 <span class="keyword">if</span>(ftbverbose);
0250   fprintf([<span class="string">'end   : '</span> subname <span class="string">'\n'</span>])
0251 <span class="keyword">end</span>;
0252 fclose(fid);</pre></div>
<hr><address>Generated on Wed 02-Nov-2011 21:58:00 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" title="Matlab Documentation in HTML">m2html</a></strong> &copy; 2005</address>
</body>
</html>